

import utilfuncs from "../../common/tools/utilfuncs";
var funcs = utilfuncs.UtilFuncs;

import {LGXEnums}  from "../../common/sysenum";


var AlignItemToContainerSvc = {}

//容器子项模具，放置在画布时，尽量放在同类型的容器模具内，形成其子项
AlignItemToContainerSvc.layContainerItemGraphInContainer = function (pGraph, mousePoint, dataManger) {
    let symbol = pGraph.getSymbol();
    if (!symbol) {
        return;
    }

    let packegGraph = this.findPackageGraph(pGraph, mousePoint, dataManger);
    if (packegGraph) {
        this.layGraphInPackage(packegGraph, pGraph, mousePoint, dataManger);
    }
    else {
        this.layItemGraphInContainer(pGraph, mousePoint, dataManger);
    }
}

AlignItemToContainerSvc.layGraphInPackage = function (packegGraph, pGraph, mousePoint, dataManger) {
    let rect = pGraph.getZone();
    let packRect = packegGraph.getZone();
    if (!funcs.chkPtInRect(rect.ltPt(), packRect)
        || !funcs.chkPtInRect(rect.rtPt(), packRect)
        || !funcs.chkPtInRect(rect.rbPt(), packRect)
        || !funcs.chkPtInRect(rect.lbPt(), packRect)) {
        return;
    }
    packegGraph.addChild(pGraph);
}

AlignItemToContainerSvc.layItemGraphInContainer = function (pGraph, mousePoint, dataManger) {
    let symbol = pGraph.getSymbol();
    let moldActionType = symbol.getMoldActionType();
    if (moldActionType != LGXEnums.moldActionType.item) {
        return;
    }
    let mainType = funcs.mainObjType(pGraph.getType());
    let containerGraph = this.findContainerGraph(pGraph, mousePoint, mainType, dataManger);
    if (containerGraph) {
        if (!containerGraph.hasChild(pGraph)) {
            let mapscale = dataManger.getMapScale();
            let verticalAlignItemGraph = containerGraph.findVerticalAlignItemGraph(pGraph.getZone().top)
            let bottomItemGraph = pGraph;
            if (verticalAlignItemGraph && verticalAlignItemGraph != pGraph) {
                bottomItemGraph = this.verticalAlign2Graph(containerGraph,pGraph, verticalAlignItemGraph, mapscale);
            }
            containerGraph.addChild(pGraph);
            this.assignChildNameIdx(containerGraph,pGraph);
            let algin2ItemBottomFlag = symbol.getAlgin2ItemBottomFlag();
            if (algin2ItemBottomFlag) {
                this.reCorrectContainerBottom(containerGraph, bottomItemGraph.getZone().bottom);
            }
        }
        else {
            this.reCorrectContainerPosition(containerGraph, pGraph);
        }
    }
    else{
        containerGraph = pGraph.getContainer();
        if (containerGraph){
            this.reCorrectContainerPosition(containerGraph, pGraph);
        }
    }
    
}

AlignItemToContainerSvc.assignChildNameIdx = function(containerGraph,pGraph){
    let childCount = containerGraph.getItemChildCount();
    let childTitle = pGraph.getTitleText();
    if (childTitle){
        let txtContent = "成员"+childCount;
        childTitle.setText(txtContent);
    }
}

AlignItemToContainerSvc.reCorrectContainerPosition = function (containerGraph, pCurentGraph) {
    let rect = pCurentGraph.getZone();
    let removeFromContainer = false;
    let containerRect = containerGraph.getZone();
    if (!funcs.chkPtInRect(rect.ltPt(), containerRect)
        || !funcs.chkPtInRect(rect.rtPt(), containerRect)
        || !funcs.chkPtInRect(rect.rbPt(), containerRect)
        || !funcs.chkPtInRect(rect.lbPt(), containerRect)) {
        removeFromContainer = true;
    }
    if (removeFromContainer){
        containerGraph.removeChild(pCurentGraph);
    }
    let titleZoneRectGraph = containerGraph.getTitleZoneRect();
    if (!titleZoneRectGraph){
        return ;
    }

    let childList = containerGraph.getChildList();
    let validItemChildList = [];
    childList.forEach(ele => {
        if (ele.chkIsContainerItem()){
            let top = ele.getZone().top;
            let t = {graph:ele,top};
            validItemChildList.push(t);
        }
    });

    if (validItemChildList.length < 2){
        return ;
    }

    let sortedItemChildList = validItemChildList.sort(function (p1, p2) {
        if ((p1.top - p2.top) < 0) {
            return 1;
        }
        else {
            return -1;
        }
    }
    );
    let mapscale = 1;
    let dstGraph = null;
    let preBottom = titleZoneRectGraph.getZone().bottom;
    for (let i = 0;i < sortedItemChildList.length;i++){
        let t = sortedItemChildList[i];
        dstGraph = t.graph;
        let tx = 0;
        let ty = preBottom - dstGraph.getZone().top;
        dstGraph.translate({x:tx,y:ty},mapscale,false);
        preBottom = dstGraph.getZone().bottom;
    }
    if (dstGraph){
        this.reCorrectContainerBottom(containerGraph, dstGraph.getZone().bottom);
    }
}

AlignItemToContainerSvc.reCorrectContainerBottom = function (containerGraph, bottomItemBottom) {
    let containerFrameRect = null;
    let childList = containerGraph.getChildList();
    for (let i = 0; i < childList.length; i++) {
        let p = childList[i];
        if (p.m_graphType == LGXEnums.LGXGraphType.RectGraph_type) {
            if (p.getSymbolEdgeFlag()) {
                containerFrameRect = p;
            }
        }
    }

    if (containerFrameRect) {
        let origRect = containerFrameRect.getZone();
        let ltPt = origRect.ltPt();
        let rtPt = origRect.rtPt();
        let p1 = { x: ltPt.x, y: ltPt.y };
        let p2 = { x: rtPt.x, y: rtPt.y };
        let p3 = { x: rtPt.x, y: bottomItemBottom };
        let p4 = { x: ltPt.x, y: bottomItemBottom };
        let position = containerFrameRect.getPosCopy();
        position.clear();
        position.addPoint(p1);
        position.addPoint(p2);
        position.addPoint(p3);
        position.addPoint(p4);
        containerFrameRect.setPos(position);

        containerGraph.reCalScopeEncloseAllChild(1);
    }
}

AlignItemToContainerSvc.verticalAlign2Graph = function (containerGraph,curentGraph, destGraph, mapscale) {
    let curentTop = curentGraph.getZone().top;
    let dstBottom = destGraph.getZone().bottom;
    let ty = dstBottom - curentTop;

    let curentLeft = curentGraph.getZone().left;
    let destLeft = destGraph.getZone().left;
    let tx = destLeft - curentLeft;

    curentGraph.translate({ x: tx, y: ty }, mapscale, false);

    //把原来位于destGraph下方的子图形全部下移
    let belowDestGraphOffsetY = curentGraph.getZone().bottom - dstBottom;
    let belowDestItemList = [];
    let itemList = containerGraph.getItemChildList();
    itemList.forEach(ele => {
        let cntPt = ele.getZone().getCnt();
        if (cntPt.y < dstBottom){
            belowDestItemList.push(ele);
        }
    });

    belowDestItemList.forEach(ele => {
        ele.translate({ x: 0, y: belowDestGraphOffsetY }, mapscale, false);
    });

    let bottomItemGraph = curentGraph;
    if (belowDestItemList.length > 0){
        let minBottomGraph = belowDestItemList[0];
        let minBottom = minBottomGraph.getZone().bottom;
        for (let i = 1;i < belowDestItemList.length;i++){
            let p = belowDestItemList[i];
            if (p.getZone().bottom < minBottom){
                minBottomGraph = p;
                minBottom = minBottomGraph.getZone().bottom;
            }
        }
        bottomItemGraph = minBottomGraph;
    }
    return bottomItemGraph;
}

AlignItemToContainerSvc.findContainerGraph = function (pCurentGraph, mousePoint, itemMainType, dataManger) {
    let destContainerGraph = null;
    let curentRect = pCurentGraph.getZone();
    let tAlllGraphList = dataManger.getAllGraphs();
    let count = tAlllGraphList.length;
    for (let i = 0; i < count; i++) {
        let pGraph = tAlllGraphList[i];
        if (pGraph == pCurentGraph
            || pGraph.getUseful() != 1) {
            continue;
        }
        let mainType = funcs.mainObjType(pGraph.getType());
        if (mainType == itemMainType) {
            let symbol = pGraph.getSymbol();
            if (symbol) {
                let moldActionType = symbol.getMoldActionType();
                if (moldActionType == LGXEnums.moldActionType.container) {
                    let rect = pGraph.getZone();
                    if (funcs.chkOverlap(curentRect, rect)) {
                        destContainerGraph = pGraph;
                        break;
                    }
                }
            }
        }
    }
    return destContainerGraph;
}

AlignItemToContainerSvc.findPackageGraph = function (pCurentGraph, mousePoint, dataManger) {
    let destContainerGraph = null;
    let tAlllGraphList = dataManger.getAllGraphs();
    let count = tAlllGraphList.length;
    for (let i = 0; i < count; i++) {
        let pGraph = tAlllGraphList[i];
        if (pGraph == pCurentGraph
            || pGraph.getUseful() != 1) {
            continue;
        }
        let symbol = pGraph.getSymbol();
        if (symbol) {
            let moldActionType = symbol.getMoldActionType();
            if (moldActionType == LGXEnums.moldActionType.packege) {
                let rect = pGraph.getZone();
                if (funcs.chkPtInRect(mousePoint, rect)) {
                    destContainerGraph = pGraph;
                    break;
                }
            }
        }
    }
    return destContainerGraph;
}

export { AlignItemToContainerSvc }